package com.gdglc.spring.biz.impl.auth;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import com.gdglc.spring.biz.auth.UserroleInfoBiz;
import com.y2.authprj.bean.auth.model.Page;
import com.y2.authprj.bean.auth.model.UserroleInfo;
import com.y2.authprj.bean.auth.vo.ShowUserroleVo;
import com.y2.authprj.bean.auth.vo.UserroleVo;
import com.y2.authprj.dao.auth.UserroleInfoDao;
import com.y2.authprj.exception.BizException;

public class UserroleInfoBizImpl  implements UserroleInfoBiz{

	private UserroleInfoDao dao;

	public UserroleInfoDao getDao() {
		return dao;
	}

	public void setDao(UserroleInfoDao dao) {
		this.dao = dao;
	}

	@Override
	public List<UserroleInfo> findAll()  throws BizException{
		return dao.findAll();
	}

	@Override
	public Page<ShowUserroleVo> findByUserroleVo(UserroleVo vo)  throws BizException{
		Page<ShowUserroleVo> userrolePage=new Page<ShowUserroleVo>();
		try{
			List<ShowUserroleVo> infoList = dao.findByUserroleVo(vo);
			int count=dao.findAllCount(vo);
			userrolePage.setTotalCount(count);
			userrolePage.setPageSize(vo.getNum());
			userrolePage.setCurrPageNo(vo.getPageNum());
			int totalPage=(count%vo.getNum()==0)?(count/vo.getNum()):(count/vo.getNum())+1;
			//总页数
			userrolePage.setTotalPageCount(totalPage);
			if(null==infoList){
				infoList = new ArrayList<ShowUserroleVo>();
			}
			userrolePage.setList(infoList);
			return userrolePage;
		}catch(Exception e){
			e.printStackTrace();
			throw new BizException("查询失败",e);
		}

	}

	@Override
	public UserroleInfo findById(Integer id)  throws BizException{
		if(null==id || id<=0){
			throw new BizException("指定需要的记录");
		}
		UserroleInfo Info=dao.findById(id);
		if(null==Info || Info.getUserroleId()!=id){
			throw new BizException("获取不到指定记录的信息，请确认是否已经被删除");
		}
		return Info;
	}

	@Override
	public void deleteUserroleInfoById(Integer id)  throws BizException{
		if(null==id || id<=0){
			throw new BizException("指定需要删除的记录");
		}
		try{
			dao.deleteUserroleInfoById(id);
		}catch(Exception e){
			throw new BizException("删除失败",e);
		}
		
	}

	
	@Override
	public void addUserroleInfo(UserroleInfo info) throws BizException{
		info.setCreateTime(new Date());
		//info.setUserroleId(userroleId);
		info.setDeleteFlag(1);
		List<UserroleInfo> infoList=dao.findAll();
		for (UserroleInfo userroleInfo : infoList) {
			if(info.getStaffId()==userroleInfo.getStaffId() &&
					info.getRoleId()==userroleInfo.getRoleId()	){
				throw new BizException("该用户已有角色，不能重复添加");
			}
		}
		try{
			dao.addUserroleInfo(info);
		}catch(Exception e){
			throw new BizException("添加失败",e);
		}	
	}

	
	
	
	
	@Override
	public void updateUserroleInfo(UserroleInfo info) throws BizException{
		info.setCreateTime(new Date());
		info.setDeleteFlag(1);
		int i = 0;
		
		try{
			List<UserroleInfo> infoList=dao.findAll();
			for (UserroleInfo userroleInfo : infoList) {
				if(info.getStaffId()==userroleInfo.getStaffId() &&
				info.getRoleId()==userroleInfo.getRoleId()	){
					throw new BizException("该用户已有角色，不能重复添加");
				}
			}
			i =dao.updateUserroleInfo(info);
		}catch(Exception e){
			throw new BizException("修改失败",e);
		}
		if(i==0){
			throw new BizException("您修改的记录已经不存在");
		}
	}

	@Override
	public void deleteUserroleInfoByRoleId(Integer roleId) throws BizException {
		if(null==roleId || roleId<=0){
			throw new BizException("指定需要删除的记录");
		}
		try{
			dao.deleteUserroleInfoByRoleId(roleId);
		}catch(Exception e){
			throw new BizException("删除失败",e);
		}
		
	}

	@Override
	public void deleteUserroleInfoByStaffId(Integer staffId) throws BizException {
		if(null==staffId || staffId<=0){
			throw new BizException("指定需要删除的记录");
		}
		try{
			dao.deleteUserroleInfoByStaffId(staffId);
		}catch(Exception e){
			throw new BizException("删除失败",e);
		}
		
	}

	@Override
	public List<UserroleInfo> findByStaffId(Integer staffId) throws BizException {
		if(0==staffId || null==staffId){
			throw new BizException("用户Id不正确，请核对！");
		}
		
		try {
			List<UserroleInfo> infoList = dao.findByStaffId(staffId);
			if(null==infoList){
				infoList = new ArrayList<UserroleInfo>();
			}
			
			return infoList;
		} catch (Exception e) {
			throw new BizException("查询失败！");
		}
	}

	@Override
	public void addByStaff(Integer staffId,Integer[] roleId) throws BizException {

		dao.deleteUserroleInfoByStaffId(staffId);
		
		List<UserroleInfo> infoList=dao.findAll();
		for (UserroleInfo userroleInfo : infoList) {
			for(Integer rId:roleId){
				if(staffId==userroleInfo.getStaffId()&&
					rId==userroleInfo.getRoleId()){
					throw new BizException("该用户已有角色，不能重复添加");
				}
			}
			
		}
		for(Integer rId:roleId){
			UserroleInfo info=new UserroleInfo();
			info.setCreateTime(new Date());
			info.setDeleteFlag(1);
			info.setRoleId(rId);
			info.setStaffId(staffId);	
			try{
				dao.addUserroleInfo(info);
			}catch(Exception e){
				throw new BizException("添加失败",e);
			}	
		}	
		
		
	}

}